package org.colcon.web.client.ui.panel;

import org.colcon.web.client.Collective_Congress;
import org.colcon.web.client.ui.ColConPanel;
import org.colcon.web.model.User;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class LoginPanel extends ColConPanel
{
	private VerticalPanel loginPanel = new VerticalPanel();
	
	private Grid grid = new Grid(3, 2);
	
	private Label nameLabel = new Label("Username:");
	private TextBox nameBox = new TextBox();
	private Label passwordLabel = new Label("Password:");
	private PasswordTextBox passwordBox = new PasswordTextBox();
	private Button button = new Button("Login");
	
	public LoginPanel(Collective_Congress client)
	{
		super(client, null, null);
		
		setStyleName("register");
		
		loginPanel.setWidth("100%");
		loginPanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
		
		button.addClickHandler(new MyButtonHandler());
		
		sinkEvents(Event.ONKEYPRESS);
		addHandler(new MyEnterHandler(), KeyPressEvent.getType());
		
		grid.setWidget(0, 0, nameLabel);
		grid.setWidget(0, 1, nameBox);
		grid.setWidget(1, 0, passwordLabel);
		grid.setWidget(1, 1, passwordBox);
		grid.setWidget(2, 1, button);

		loginPanel.add(grid);
		add(loginPanel);
		
		setFocus(nameBox);
	}
	
	private class MyButtonHandler implements ClickHandler
	{
		public void onClick(ClickEvent event) 
		{
			buttonClick();
		}
	}
	
	private class MyEnterHandler implements KeyPressHandler
	{
		public void onKeyPress(KeyPressEvent event) 
		{
			if(event.getCharCode() == KeyCodes.KEY_ENTER)
				buttonClick();
		}
	}
	
	public void buttonClick()
	{
		if(!validate())
		{
			loginPanel.clear();
			loginPanel.add(grid);
			
			loginPanel.add(new HTML("Invalid input"));
			return;
		}
		
		loginPanel.clear();
		loginPanel.add(new HTML("Loading..."));
		
		getService().authenticate(nameBox.getText(),  passwordBox.getText(), new AsyncCallback<User>()
		{
			public void onFailure(Throwable caught)
			{
				loginPanel.clear();
				loginPanel.add(grid);
				caught.printStackTrace();
				loginPanel.add(new HTML(caught.getMessage()));
			}
			
			public void onSuccess(User user)
			{
				if(user != null)
				{
					loginPanel.clear();
					loginPanel.add(new HTML("Login successful"));
					
					getClient().login(user);
					getClient().refresh();
					
					getBodyPanel().clear();
					getBodyPanel().add(new UserPanel(getClient(), getClient().getUser()));
				}
				else
				{
					loginPanel.clear();
					loginPanel.add(grid);
					loginPanel.add(new HTML("Login failed"));
				}
			}
		});
	}
	
	private boolean validate()
	{
		return
			!(
				nameBox.getText().isEmpty() ||	
				passwordBox.getText().isEmpty()
			);
	}
}
